package com.sign2024.sign2024project.capitalconstruction.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.capitalconstruction.JjPlanInventory;
import com.sign2024.sign2024core.entry.projecttask.ProjectTask;
import com.sign2024.sign2024project.aop.listener.jj.JjPlanExcelListener;
import com.sign2024.sign2024project.capitalconstruction.param.JjParam;
import com.sign2024.sign2024project.capitalconstruction.service.JjPlanInventoryService;
import com.sign2024.sign2024project.projecttask.service.ProjectTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 基建计划清单 控制层
 *
 * @author yujie
 * @data 2024/8/24 12:31
 */
@Api(tags = "基建")
@RestController
@RequestMapping("/api/jjPlan")
public class JjPlanInventoryController {

    @Resource
    private JjPlanInventoryService jjPlanInventoryService;

    @Resource
    private ProjectTaskService projectTaskService;


    @Log(title = "基建", businessType = "导入")
    @ApiOperation(value = "基建计划清单表excel导入", notes = "基建计划清单表excel导入")
    @Transactional
    @PostMapping("/import")
    public CommonResult<JjPlanInventory> importInfo(MultipartFile file, Integer taskId) {
        if (StringUtils.isNull(taskId)) {
            throw new CommonException(CommonConstants.ERROR, "任务标识为空");
        }

        // 删除该任务下的数据
        jjPlanInventoryService.deleteByTaskId(taskId);
        // 将 任务 id 进行赋值
        JjPlanExcelListener listener = new JjPlanExcelListener();
        listener.setTaskId(taskId);
        try {
            EasyExcel.read(file.getInputStream(), listener).head(JjPlanInventory.class).sheet().doReadSync();

            // 导入数据后， 将任务中的 基建计划清单 列修改为 已导入
            ProjectTask projectTask = new ProjectTask();
            projectTask.setId(taskId);
            projectTask.setPlanInventory("已导入");
            projectTaskService.updateTask(projectTask);

            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @Log(title = "基建", businessType = "查询")
    @ApiOperation(value = "分页查询基建计划清单", notes = "分页查询基建计划清单")
    @PostMapping("/page")
    public CommonResult<JjPlanInventory> page(JjParam param) {
        Page<JjPlanInventory> page = jjPlanInventoryService.pageJjPlan(param);
        return CommonResult.success(page);
    }

}
